package DAO;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import Hibernate.HibernateUtil;
import Negocio.Remito;

public class HibernateRemitoDAO {
	
	private static HibernateRemitoDAO instancia = null;
	private static SessionFactory sf = null;
	
	
	public static HibernateRemitoDAO getInstancia(){
		if(instancia == null){
			instancia = new HibernateRemitoDAO();
			sf = HibernateUtil.getSessionFactory();
		}
		return instancia;
	}
	
	public void grabarRemito(Remito r){
		try{
		Session session = sf.openSession();
		session.beginTransaction();
		session.saveOrUpdate(r);
		session.flush();
		session.getTransaction().commit();
		session.close();
		}catch(Exception e){
			System.out.println(e);
			System.out.println("Error al grabar un Remito");
		}
	}
	
	public void updateRemito(Remito r){
		try{
		Session session = sf.openSession();
		session.beginTransaction();
		session.saveOrUpdate(r);
		session.flush();
		session.getTransaction().commit();
		session.close();
		}catch (Exception e){
			System.out.println(e);
            System.out.println("Error al actualizar el Remito");
		}
		
	}

	
	public Remito getRemito(int id){
        try {
        	Session session=sf.openSession();
            session.beginTransaction();
            Remito r = (Remito)session.get(Remito.class, id);
            session.getTransaction().commit();
            session.close();
            return r;
        } catch (Exception e){
            System.out.println(e);
            System.out.println("Error al cargar el Remito");
        }
        return null;
	}

}
